<template>
  <div>
    <el-dialog :visible="showDialog" v-bind="$attrs" title="title" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form ref="deptForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入部门名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="code">
          <el-input v-model="formData.code" placeholder="请输入部门编号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="部门管理者" prop="manager">
          <el-select v-model="formData.manager" placeholder="请选择部门管理者" clearable :style="{width: '100%'}" @focus="getEmployeeSimple">
            <el-option
              v-for="(item,index) in peoples"
              :key="index"
              :label="item.username"
              :value="item.username"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="formData.introduce"
            type="textarea"
            placeholder="请输入部门介绍"
            :autosize="{minRows: 4, maxRows: 4}"
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { addDepartment, getDepartDetail, getDepartments, updateDepartment } from '@/api/department'
import { getEmployeeSimple } from '@/api/employees'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      // 兄弟部门中禁止出现重复部门
      // 方式1不发请求，得到兄弟
      // const brothers = this.treeNode.children
      // 方式2
      const { depts } = await getDepartments()
      let brothers
      if (this.formData.id) {
        brothers = depts.filter(t => t.pid === this
          .treeNode.pid && t.id !== this.treeNode.id)
      } else {
        brothers = depts.filter(t => t.pid === this.treeNode.id)
      }
      // 在兄弟当中存不存在有一个同名的部门
      const isExist = brothers.some(t => t.name === value)
      if (isExist) {
        callback('存在同名的兄弟们')
      } else {
        callback()
      }
    }
    const checkCode = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isExist
      if (this.formData.id) {
        // 编辑
        isExist = depts.filter(t => t.id !== this.treeNode.id).some(t => t.code === value)
      } else {
        isExist = depts.some(t => t.code === value)
      }
      if (isExist) {
        callback('存在同编号的部门')
      } else {
        callback()
      }
    }
    return {
      formData: {
        name: undefined,
        code: undefined,
        manager: undefined,
        introduce: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入部门名称',
          trigger: 'blur'
        }, {
          min: 1,
          max: 50,
          message: '请输入1-50个字符',
          trigger: 'blur'
        }, {
          validator: checkName,
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入部门编号',
          trigger: 'blur'
        }, {
          min: 1,
          max: 50,
          message: '请输入1-50个字符',
          trigger: 'blur'
        }, {
          validator: checkCode,
          trigger: 'blur'
        }],
        manager: [{
          required: true,
          message: '请输入部门管理者',
          trigger: 'blur'
        }],
        introduce: [{
          required: true,
          message: '请输入部门介绍',
          trigger: 'blur'
        }, {
          min: 1,
          max: 300,
          message: '请输入1-300个字符',
          trigger: 'blur'
        }]
      },
      peoples: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getDepartDetail(id) {
      const formData = await getDepartDetail(id)
      this.formData = formData
    },
    async getEmployeeSimple() {
      // console.log(1111)
      this.peoples = await getEmployeeSimple()
    },
    // async getElementById() {
    //   this.people = await this.getElementById()
    // },
    onOpen() {},
    onClose() {
      this.close()
    },
    close() {
      this.formData = this.$options.data().formData
      this.$refs['deptForm'].resetFields()
      this.$emit('update:showDialog', false)
    },
    handelConfirm() {
      this.$refs['deptForm'].validate(async valid => {
        if (!valid) return
        // 发请求
        if (this.formData.id) {
          // 编辑
          await updateDepartment(this.formData)
        } else {
          // 新增
          await addDepartment({ ...this.formData, pid: this.treeNode.id })
        }
        // 通知父组件刷新
        this.$emit('addDept')
        // 关闭对话框
        this.close()
      })
    }
  }

}

</script>
<style>
</style>
